package com.eascs.web.o2o.web.controller;

import java.text.DecimalFormat;
import java.text.Format;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.common.util.UrlUtil;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.PaginationRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.web.o2o.web.form.LoanBalanceForm;
import com.eascs.web.o2o.web.service.LoanBalanceService;
import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.service.sys.SysOrgService;
import com.eascs.workflow.common.vo.ModelResult;

/**
 * 银行申报进度跟进表
 */
@RestController
@RequestMapping("/restBalance")
public class LoanBalanceRestController extends BaseRestController {
	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Reference
	private SysOrgService sysOrgService;

	@Autowired
	LoanBalanceService loanBalanceService;
	
	@RequestMapping(value = "/search")
	public BaseRestResult<List<Object[]>> units(String platName) {
		List<Object[]> data = new ArrayList<>();
		if (StringUtil.hasText(platName)) {
			ModelResult<List<Object[]>> da = sysOrgService.getOrgListLikeName(platName,ActOrgEntity.ORG_LEVEL_PLATFORM, UserHolder.getUser());
			data = da.getModel();
		}
		return BaseRestResult.buildRestResult(data);
	}
	
	
	@RequestMapping("/platRestPage")
 	public PaginationRestResult platRestPage(LoanBalanceForm form) {
 		String o2ourl = "/restBalance/platRestPage";
 		if(form!=null){
			String ltd = form.getLtd()==null?"":form.getLtd();
			String selectDate = form.getSelectDate();
			o2ourl = UrlUtil.appendUrl(o2ourl, "ltd",ltd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "selectDate",selectDate);
			o2ourl=o2ourl+"&showPage=";
		}else{
			o2ourl= o2ourl+"?showPage=";
		}
 		form.setPageSize(10000);
		PageResult<Object[]> pageResult    = loanBalanceService.LoanBalancePlatProgress(form);
 		String titles[] = {"省区","地级市","平台","放款金额","银行正常贷款余额", "银行逾期贷款余额", "代偿贷款金额", "代偿贷款欠还余额"};
 		SelectDataOption[] selectDataOptions = {null,null, null,null,null, null, null, null};
 		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
 		o2oPagingInfo.setBuildPagingNavigation(false);
 		o2oPagingInfo.setFirstRenderColumnIndex(0);
 		o2oPagingInfo.setSelectDataOptions(selectDataOptions);
 		o2oPagingInfo.setFormats(new Format[] { null,null, null,  new DecimalFormat(",##0.00"),  new DecimalFormat(",##0.00") , new DecimalFormat(",##0.00"), new DecimalFormat(",##0.00"), new DecimalFormat(",##0.00")});
 		o2oPagingInfo.setRendTo("#platRest_result");
 		ViewContext.put("vo", form);
 		return PaginationRestResult.buildPaginationRestResult(o2oPagingInfo);
 	}

	
	
	@RequestMapping("/cityRestPage")
 	public PaginationRestResult cityRestPage(LoanBalanceForm form) {
 		String o2ourl = "/restBalance/cityRestPage";
 		if(form!=null){
			String city = form.getCity();
			String province = form.getProvince();
			String selectDate = form.getSelectDate();
			o2ourl = UrlUtil.appendUrl(o2ourl, "city",city);
			o2ourl = UrlUtil.appendUrl(o2ourl, "province",province);
			o2ourl = UrlUtil.appendUrl(o2ourl, "selectDate",selectDate);
			o2ourl=o2ourl+"&showPage=";
		}else{
			o2ourl= o2ourl+"?showPage=";
		}
 		form.setPageSize(10000);
		PageResult<Object[]> pageResult    = loanBalanceService.LoanBalanceCityProgress(form);
 		String titles[] = {"省区","地级市","放款金额","银行正常贷款余额", "银行逾期贷款余额", "代偿贷款金额", "代偿贷款欠还余额"};
 		SelectDataOption[] selectDataOptions = { null,null,null,null, null, null, null};
 		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
 		o2oPagingInfo.setBuildPagingNavigation(false);
 		o2oPagingInfo.setFirstRenderColumnIndex(0);
 		o2oPagingInfo.setSelectDataOptions(selectDataOptions);
 		o2oPagingInfo.setFormats(new Format[] {  null,null,  new DecimalFormat(",##0.00"),  new DecimalFormat(",##0.00") , new DecimalFormat(",##0.00"), new DecimalFormat(",##0.00"), new DecimalFormat(",##0.00")});
 		o2oPagingInfo.setRendTo("#cityRest_result");
 		ViewContext.put("vo", form);
 		return PaginationRestResult.buildPaginationRestResult(o2oPagingInfo);
 	}
	
	
	@RequestMapping("/provinceRestPage")
 	public PaginationRestResult provinceRestPage(LoanBalanceForm form) {
 		String o2ourl = "/restBalance/provinceRestPage";
 		if(form!=null){
			String province = form.getProvince();
			String selectDate = form.getSelectDate();
			o2ourl = UrlUtil.appendUrl(o2ourl, "province",province);
			o2ourl = UrlUtil.appendUrl(o2ourl, "selectDate",selectDate);
			o2ourl=o2ourl+"&showPage=";
		}else{
			o2ourl= o2ourl+"?showPage=";
		}
 		form.setPageSize(10000);
		PageResult<Object[]> pageResult    = loanBalanceService.LoanBalanceProvinceProgress(form);
 		String titles[] = {"省区","放款金额","银行正常贷款余额", "银行逾期贷款余额", "代偿贷款金额", "代偿贷款欠还余额"};
 		SelectDataOption[] selectDataOptions = { null,null,null, null, null, null};
 		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
 		o2oPagingInfo.setBuildPagingNavigation(false);
 		o2oPagingInfo.setFirstRenderColumnIndex(0);
 		o2oPagingInfo.setSelectDataOptions(selectDataOptions);
 		o2oPagingInfo.setFormats(new Format[] {  null,  new DecimalFormat(",##0.00"),  new DecimalFormat(",##0.00") , new DecimalFormat(",##0.00"), new DecimalFormat(",##0.00"), new DecimalFormat(",##0.00")});
 		o2oPagingInfo.setRendTo("#provinceRest_result");
 		ViewContext.put("vo", form);
 		return PaginationRestResult.buildPaginationRestResult(o2oPagingInfo);
 	}
	
	
	
	
	
	
	
}
